<?php

namespace app\common\service\user;

use app\admin\model\store\GoodsCouponIssueUser;
use app\admin\model\user\User;
use app\common\service\BaseService;
use app\common\service\lib\QrcodeImg;

class UserService extends BaseService
{
    /**
     * Author:Zhongyuan Lu
     * 根据id修改数据
     * @param $id
     * @param $password
     * @return array
     */
    public static function updateOne($id, $params)
    {
        if (empty($id) || !is_int($id) || $id < 1) {
            return self::fail('参数错误');
        }

        $bool = User::updateOne($id, $params);

        if ($bool !== false) {
            return self::success('设置成功', []);
        }
        return self::fail('设置失败');
    }

    public static function shareInvitationCode($id)
    {
        $id = 17;
        $spread_code = User::where('id',$id)->value('spread_code');
        $url = config('setting.domain.isRegister')."spread_code={$spread_code}";
        $qrcode = new QrcodeImg();
        $image = $qrcode->index($url);
        dump($image);
    }

    /**
     * Author:Yingbo Sun
     * 获取用户信息
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserInfo($uid)
    {
        if (empty($uid)){
            return self::fail('参数错误');
        }
        $field = ['id','username','nickname','phone','avatar','money','integral'];
        $user = User::where('id',$uid)->field($field)->find();
        $user && $user = $user->toArray();
        $user['id'] = encrypt($user['id']);
        $where[] = ['uid','=',$uid];
        $where[] = ['status','=',0];
        $user['coupon'] = GoodsCouponIssueUser::where($where)->count();
        if ($user){
            return self::success('获取成功',$user);
        }else{
            return self::fail('获取失败');
        }
    }
}